﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using HelfenHelfen.RIA.Web.Models;
using Microsoft.Maps.MapControl;

namespace HelfenHelfen.RIA.Views
{
    public partial class DemandList : Page
    {
        public DemandList()
        {
            InitializeComponent();

            this.demandDomainDataSource.SubmittedChanges += new EventHandler<SubmittedChangesEventArgs>(demandDomainDataSource_SubmittedChanges);
        }

        void demandDomainDataSource_SubmittedChanges(object sender, SubmittedChangesEventArgs e)
        {
            MessageBox.Show("Changes saved!");
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {

        }

        private void demandDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void btnDetails_Click(object sender, RoutedEventArgs e)
        {
            var d = this.demandDomainDataSource.DataView.CurrentItem as Demand;
            NavigationService.Navigate(new Uri(
                String.Format("/DemandDetails?ID={0}", d.DemandGUID.ToString()), 
                UriKind.Relative));
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            this.demandDomainDataSource.SubmitChanges();
        }

        private void demandDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var p = new ApplicationIdCredentialsProvider();
            p.ApplicationId = "AmEwvtuHBXciw41cousJattZogcWpJ5QDgfDq_Wv0fUEqJ9fCmyrrp8uy_tkFnlg";
            map1.CredentialsProvider = p;

            var d = this.demandDomainDataSource.DataView.CurrentItem as Demand;

            if (d != null && d.Latitude != null)
            {
                map1.SetView(new Microsoft.Maps.MapControl.Location(
                    (double)d.Latitude.Value,
                    (double)d.Longitude.Value),
                    16.0);
            }
        }

    }
}
